<!--
 * @Author: your name
 * @Date: 2021-06-24 16:12:49
 * @LastEditTime: 2021-06-28 09:30:35
 * @LastEditors: Please set LastEditors
 * @Description: 轮播配置页面
 * @FilePath: \briup\项目\briup-ui\src\pages\admin\carousel\List.vue
-->
<template>
  <!-- 轮播配置页面 -->
  <div>
    <div class="header">
      <el-button type="primary" @click="toAdd">新增</el-button>
      <el-button type="primary" @click="toPass">浏览</el-button>
    </div>
    <div class="table">
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" align="center" />
        <el-table-column prop="name" label="轮播图名称" align="center" />
        <el-table-column prop="introduce" label="轮播图描述" align="center" />
        <el-table-column prop="url" label="轮播图图片" align="center">
          <template slot-scope="scope">
            <!-- scope是当前temlate模板的作用域对象，row属性上存放的是当前行的数据对象 -->
            <el-image
              style="width: 30px; height: 20px"
              :src="scope.row.url"
              :preview-src-list="[scope.row.url]"
            />
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === '正常' ? 'success' : 'danger'"
            >{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              class="green_info"
              type="text"
              size="small"
              @click="toEdit(scope.row)"
            >编辑</el-button>
            <el-button
              class="red_info"
              type="text"
              size="small"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
            <el-button
              v-if="scope.row.status === '正常'"
              class="green_info"
              type="text"
              size="small"
              @click="toStop(scope.row.id)"
            >停用</el-button>
            <el-button
              v-else
              class="blue_info"
              type="text"
              size="small"
              @click="toStart(scope.row.id)"
            >启用</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClear">
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="轮播图片" :label-width="formLabelWidth" prop="url">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item
          label="描述"
          :label-width="formLabelWidth"
          prop="introduce"
        >
          <el-input
            v-model="form.introduce"
            autocomplete="off"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 大图预览 -->
    <el-dialog
      title="大屏预览"
      :visible.sync="visibleView"
      height="400px"
      width="1200px"
    >
      <template prop="url">
        <el-carousel :interval="5000" arrow="always">
          <el-carousel-item v-for="item in browseData" :key="item.id">
            <el-image style="width: 100%; height: 100%" :src="item.url" />
          </el-carousel-item>
        </el-carousel>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visibleView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  deletecarousel,
  findAllcarousel,
  saveOrUpdatecarousel
} from '@/api/carousel'
import { showFileURL, uploadFileURL } from '@/utils/config'
export default {
  data() {
    return {
      tableData: [],
      loading: false,
      form: {},
      title: '',
      formLabelWidth: '80px',
      visible: false,
      imageUrl: '',
      showFileURL,
      uploadFileURL,
      rules: {
        name: [
          { required: true, message: '请输入轮播图名称', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入轮播图名称', trigger: 'change' }
        ]
      },
      browseData: [],
      visibleView: false
    }
  },
  computed: {},
  created() {
    this.querytableData()
  },
  mounted() {},
  methods: {
    async querytableData() {
      this.loading = true
      const res = await findAllcarousel()
      this.tableData = res.data
      this.loading = false
      this.browseData = res.data
    },
    toEdit(row) {
      this.querytableData()
      this.form = { ...row }
      this.imageUrl = this.form.url
      this.title = '编辑轮播图'
      this.visible = true
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deletecarousel({ id })
          this.$message({ type: 'success', message: res.message })
          this.querytableData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async toStop(id) {
      const res = await saveOrUpdatecarousel({ id, status: '停用' })
      this.$notify.success({
        title: '成功',
        message: res.message
      })
      this.querytableData()
    },
    async toStart(id) {
      const res = await saveOrUpdatecarousel({ id, status: '正常' })
      this.$notify.success({
        title: '成功',
        message: res.message
      })
      this.querytableData()
    },
    toAdd() {
      this.querytableData()
      this.form = {}
      this.title = '新增轮播图'
      this.imageUrl = ''
      this.visible = true
    },
    toPass() {
      this.visibleView = true
    },
    // 图片上传成功的回调函数
    handleAvatarSuccess(res, file) {
      // console.log(res,"==图片上传成功之后的res");
      this.imageUrl = URL.createObjectURL(file.raw)
      // console.log(this.imageUrl);
      this.form.url = showFileURL + res.data.id
    },
    // 图片上传之前的回调函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    toSave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdatecarousel(this.form)
          this.$notify.success({
            title: '成功',
            message: res.message
          })
          this.querytableData()
          this.visible = false
        } else {
          return false
        }
      })
    },
    toClear() {
      this.$refs.ruleForm.resetFields()
      setTimeout(() => {
        this.visible = false
      }, 100)
    }
  }
}
</script>
<style lang="scss" scoped>
.header {
  display: flex;
  .el-button {
    margin-right: 10px;
  }
}
</style>
